package com.wzy.spider.util;

import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.Site;
import us.codecraft.webmagic.Spider;
import us.codecraft.webmagic.processor.PageProcessor;

public class YXPageProcessor implements PageProcessor {
    @Override
    public void process(Page page) {
        Document document = page.getHtml().getDocument();
        Elements cars = document.select(".ab_carlist ul li");
        for (Element car : cars) {
            Element img = car.select(".across>a>img").get(0);
            String imageUrl = img.attr("data-original");
            String title = img.attr("alt");
            String price = car.select(".pad>p>em").get(0).text();
            String text = car.select(".pad>span").get(0).text();
            System.out.println(imageUrl);
            System.out.println(title);
            System.out.println(price);
            System.out.println(text);
        }
    }

    Site site = Site.me()
            .setTimeOut(5000)
            .setUserAgent("Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Mobile Safari/537.36");

    @Override
    public Site getSite() {
        return site;
    }

    public static void main(String[] args) {
        Spider.create(new YXPageProcessor())
                .addUrl("https://www.xin.com/zibo/sn_p9-15/i1/")
                .start();
    }
}
